package at.ac.lbg.media.vis.client.theme.manager
{
	
	import at.ac.lbg.media.vis.client.theme.operator.CustomLabeler;
	import at.ac.lbg.media.vis.client.theme.render.IArtworkRenderer;
	import at.ac.lbg.media.vis.client.theme.view.Artwork;
	
	import flare.display.TextSprite;
	import flare.vis.Visualization;
	import flare.vis.controls.ClickControl;
	import flare.vis.controls.HoverControl;
	import flare.vis.data.Data;
	import flare.vis.data.DataSprite;
	import flare.vis.events.SelectionEvent;
	import flare.vis.operator.layout.AxisLayout;
	
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.controls.List;
	import mx.core.Application;
	
	public class LandscapeManagerHexboard {
		
		public static var LABELER:String = "labeler";
		public var artworkRenderer:IArtworkRenderer;
		
		private var info1:TextSprite;
		
		public function LandscapeManagerHexboard() {
			// quasi dependency injection with spring actionscript
			artworkRenderer = Application.application.applicationContext.getObject("artworkRenderer");
		}
		
		public function initLandscape(viz:Visualization, data:ArrayCollection):void {//, awdC:ArrayCollection):void {
			var catDict:Dictionary = Application.application.catDict;
			var legend:List = Application.application.legend;
			//trace("initLandscape "+data.length);
			viz.data = convertData(data);
			
			// define the visual encodings
			viz.data.nodes.setProperties({
				fillColor: 0x018888ff, // transparent fill to catch mouse hits
				lineWidth: 2
			});
			
			// define x and y fields from which vis should operate
			var field1:String = "origX";
			var field2:String = "origY";
			var layout:AxisLayout = new AxisLayout(field1, field2);
			viz.operators.add(layout);
			viz.xyAxes.xAxis.fixLabelOverlap = false; // keep overlapping labels
			viz.axes.alpha = 255;
			
			// Labels for each artwork
			
			var labeler:CustomLabeler = new CustomLabeler("title", null, new TextFormat("Arial", "11", "0x336633"), function(item:Artwork):Boolean{ return item.stackCount < 2 || item.stack }, CustomLabeler.CHILD);			
			labeler.xOffset = -25;
			labeler.yOffset = 25;
			labeler.horizontalAnchor = TextSprite.LEFT;
			labeler.textMode = TextSprite.DEVICE;
			
			viz.operators.add(labeler);
			
			// add mouse over
			var hc:HoverControl = new HoverControl(
				DataSprite, 
				HoverControl.MOVE_TO_FRONT, 
				//roll in
				function(e:SelectionEvent):void {
					var aw:Artwork = e.item as Artwork;
					aw.origHeight = aw.height;
					aw.origWidth = aw.width;
					aw.scaleX = 1.5;
					aw.scaleY = 1.5;
					Application.application.categoryManager.handleSelectedCats(aw.categoryIds);
				},	
				//roll out			
				function(e:SelectionEvent):void {
					Application.application.stackList.visible = false;
					var aw:Artwork = e.item as Artwork;
					var sp:TextSprite = aw.getChildByName("awlabel");
					if (sp != null) {
						e.item.removeChild(sp);
					}
					
					var details:TextSprite = aw.getChildByName("details");
					if (details != null) {
						details.visible = false;
						e.item.removeChild(details);
					}
					Application.application.categoryManager.handleSelectedCats(null);
					aw.scaleX = 1.0;
					aw.scaleY = 1.0;
					
				}
			);
			
			viz.controls.add(hc);
//			trace("viz.controls.list "+viz.controls.list);
//			trace("viz.controls.getControlAt(0):  "+viz.controls.getControlAt(0));		
			

			var dm:DetailsManager = Application.application.detailsManager;
			var ccSt:ClickControl = new ClickControl(
				DataSprite,
				1);	
			ccSt.addEventListener(SelectionEvent.SELECT, dm.onSingleClick);
			viz.controls.add(ccSt);

		}
		
		public function convertData(artworks:ArrayCollection):Data {
		var data:Data = new Data();
			var count:Number = 0;
			for each(var aw:Artwork in artworks) {
				aw.renderer = artworkRenderer;
				aw.mouseChildren = false;
				aw.buttonMode = true;
				//display hex grid by moving every 2nd row down by 0.5
				if(aw.origX%2==0){
					aw.origY = aw.origY+0.5;
				}
				data.addNode(aw);
				}
			return data;
		}
	}
}